import Vue from 'vue'
import App from './App'
import store from './store'
import * as $apis from './apis/index.js'
import $mRouter from './common/router.js'
import $mUtils from './common/utils.js'
import $AppEntryController from './AppEntryController.js'
import $mConfig from "./config/index.config.js"
import $mRoutesConfig from './config/routes.config.js'
import $modalHelper from './common/modalHelper.js'

import mPageView from "./components/m-page-view/m-page-view.vue"
import musicFab from "./components/larker_shop/music-fab.vue"


Vue.component("joy-page", mPageView)
Vue.component("music-fab", musicFab)

Vue.config.productionTip = false
Vue.prototype.$store = store
Vue.prototype.$AppEntryController = $AppEntryController;
Vue.prototype.$apis = $apis;
Vue.prototype.$mRouter = $mRouter;
Vue.prototype.$mUtils = $mUtils;
Vue.prototype.$mConfig = $mConfig;
Vue.prototype.$mRoutesConfig = $mRoutesConfig;
Vue.prototype.$modalHelper = $modalHelper;
// #ifdef H5  
import $wechat from './common/wechat.js'  
//if($wechat.isWechat()){  
    Vue.prototype.$wechat = $wechat;  
//}  
// #endif  


Vue.prototype.$wx = wx //uni的全局对象

Vue.prototype.$backgroundAudioData = {
	playing: false,
	playTime: 0,
	formatedPlayTime: '00:00:00'
}

Vue.prototype.goToDetail = function(id,goodsType=3){
	var routeUrl = '';
	//商品类型    1:实物   2:虚拟类   3:视频/音频
	if(goodsType == 0){
		routeUrl = this.$mRoutesConfig.article_detail;
	}else if(goodsType == 1){
		routeUrl = this.$mRoutesConfig.audio_detail;
	}else if(goodsType == 2){
		routeUrl = this.$mRoutesConfig.audio_detail;
	}else if(goodsType == 3){
		// #ifndef H5 
		routeUrl = this.$mRoutesConfig.audio_detail;
		// #endif
		// #ifdef H5
		routeUrl = this.$mRoutesConfig.audio_detail;
		// #endif 
	}
	this.$mRouter.push({
		route: routeUrl,
		query: {
			id: id
		}
	})
}
Vue.prototype.navToCategoryPage = function(id) {
	this.$mRouter.push({
		route: this.$mRoutesConfig.category,
		query: {
			id: id
		}
	})
},
Vue.prototype.silentLogin = function(){
	uni.login({
		// #ifdef MP-WEIXIN
		provider: 'weixin',
		// #endif
		// #ifdef MP-TOUTIAO
		provider: 'toutiao',
		// #endif
		success: (res) => {
			console.log('login success:', res);
			if (res && res.code) {
				// #ifdef MP-WEIXIN
				this.silentCheckCode($apis.getWxXcxSilentCheckCode,res.code);
				// #endif
				// #ifdef MP-TOUTIAO
				this.silentCheckCode($apis.getTtXcxSilentCheckCode,res.code);
				// #endif
			}
		},
		fail: (err) => {
			console.log('login fail:', err);
		}
	});
}

Vue.prototype.silentCheckCode = async function(action,code){
	try {
		let data = null;
		data = await action({code:code});
		//data = $apis.getWxXcxSilentCheckCode({code:code});
		console.log("+++++++++++Vue.prototype.silentCheckCode+++++++++++++" + JSON.stringify(data));
		// 前端自动登录
		store.commit("SET_TOKEN", data.token);
		store.commit("SET_USERINFO", data.user || {});
		store.commit("SET_EXPIRE_TIME", data.expireTime || 0);
		store.commit("SET_GOODS_MSG_CACHE", null);
	} catch (e) {
	}
}
Vue.prototype.$await = async function(action) {
    try {
        await action();
    } catch (err) {
        // 这里是你处理错误的逻辑
        console.log("Cought Error: ", err);
    }
};


$mRouter.beforeEach((navType, to) => {
	if (to.route === undefined) throw ("路由钩子函数中没有找到to.route对象，路由信息:" + JSON.stringify(to));
	
	// 登录成功后的重定向地址和参数
	let query = {
		routesConfig: to.route,
		query:to.query
	}
	uni.setStorage({
		key: 'beforeLoginUrlQuery',
		data: query,
	});
	console.log(JSON.stringify(navType) +"===to==="+JSON.stringify(to) + "   " + store.getters.hasLogin);
	if (to.route.path === $mRoutesConfig.login.path && store.getters.hasLogin) {
		uni.redirectTo({
			url: $mUtils.objParseUrlAndParam($mRoutesConfig.main.path, to.query)
		})
		return;
	}

	// 过滤需要权限的页面
	if (to.route.requiresAuth) {
		if (store.getters.hasLogin) {
			// 已经登录
			uni[navType]({
				url: $mUtils.objParseUrlAndParam(to.route.path, to.query)
			})
		} else {
			// 没有登录 是否强制登录?
			let oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.login.path, query);
			//微信内浏览器
			// #ifdef H5  
			if($wechat.isWechat()){
			    oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.wxh5SilentLogin.path, query);  
			}
			// #endif  
			// #ifdef MP-WEIXIN
			oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.wxXcxlogin.path, query);
			// #endif
			console.log(oldUrl);
			
			if (store.state.forcedLogin) {
				uni.redirectTo({
					url: oldUrl
				})
			} else {
				uni.navigateTo({
					url: oldUrl
				})
			}
		}
	} else {
		uni[navType]({
			url: $mUtils.objParseUrlAndParam(to.route.path, to.query)
		})
	}
})
App.mpType = 'app'

const app = new Vue({
	store,
    ...App
})
app.$mount()